package features.advance.leetcode.tree.medium;

import features.advance.leetcode.tree.model.TreeNode;
import features.advance.leetcode.util.TreeUtil;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author LIN
 * @date 2021-06-21
 */
public class Offer32_1 {
    public static void main(String[] args) {
        Solution solution = new Solution() {
        };

        String str = "[3,9,20,null,null,15,7]";
        TreeNode root = TreeUtil.stringToTreeNode(str);
        int[] res = solution.levelOrder(root);
        System.out.println(Arrays.toString(res));
    }

    static class Solution {
        public int[] levelOrder(TreeNode<Integer> root) {
            if(root == null){
                return null;
            }
            Queue<TreeNode<Integer>> queue = new LinkedList<>();
            Queue<Integer> queue1 = new LinkedList<>();
            queue.add(root);

            while(!queue.isEmpty()){
                TreeNode<Integer> poll = queue.poll();
                queue1.add(poll.val);
                if(poll.left!=null){
                    queue.add(poll.left);
                }
                if(poll.right!=null){
                    queue.add(poll.right);
                }

            }
            int size = queue1.size();
            int[] ints = new int[size];
            for (int i = 0; i < size; i++) {
                ints[i] = queue1.poll();
            }
            return ints;
        }
    }

}
